/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.cd;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import who.tmap.bo.ThematicMap;
import who.tmap.bo.cd.CDDotDensity;
import who.tmap.bo.cd.CDTypes;
import who.tmap.bo.cd.CartographicDisplay;
import who.tmap.bo.cd.DotDensityDistributions;
import who.tmap.bo.cd.impl.CartographicDisplayFactory;
import who.tmap.common.TMapException;
import who.tmap.ui.util.JComboBoxColor;


/**
 * @author stl
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class JPanelCDDotDensity extends JPanel implements PanelCD {

	private static final int DEFAULT_NB_UNITS_PER_SYMBOL =20;	
	private static final int DEFAULT_POINT_SIZE =1;
	
    private static Logger logger = Logger.getLogger(JPanelCDDotDensity.class);  
	
	private ThematicMap thematicMap =null;  
	private JPanel thisPanel =this;
	private boolean dirty =false;
 
	private JComboBox jComboBoxDataColumn = null;
	private JComboBox jComboBoxColor = null;

	private JTextField jTextFieldNbUnitsPerSymbol = null;
	private JComboBox jComboBoxJoinIdColumn = null;
	
	private JTextField jTextFieldPointSize = null;
	private JPanel jPanelInfo = null;	
	
	private JComboBox jComboBoxDistribution =null;
	
	private JPanelCDHelper jPanelCDHelper =new JPanelCDHelper();  

	/**
	 * This is the default constructor
	 */
	public JPanelCDDotDensity() {
		super();
		initialize();
	}

	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(300, 200);
		this.add( getJPanelInfo(), BorderLayout.NORTH );
		this.add( Box.createVerticalGlue(), BorderLayout.CENTER );
	}	

	private JComboBox getJComboBoxDataColumn() {
		if(jComboBoxDataColumn == null) {
			jComboBoxDataColumn = new JComboBox();		
			jComboBoxDataColumn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jComboBoxDataColumn;
	}

	private JComboBox getJComboBoxColor() {
		if(jComboBoxColor == null) {
			jComboBoxColor = new JComboBoxColor();
			jComboBoxColor.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jComboBoxColor;
	}

	private JTextField getJTextFieldNbUnitsPerSymbol() {
		if(jTextFieldNbUnitsPerSymbol == null) {
			jTextFieldNbUnitsPerSymbol = new JTextField();
			jTextFieldNbUnitsPerSymbol.setText( String.valueOf( DEFAULT_NB_UNITS_PER_SYMBOL ) );
			jTextFieldNbUnitsPerSymbol.addKeyListener(new KeyAdapter() {
				public void keyTyped(KeyEvent e) {
					setDirty( true );		
				}
			});
		}
		return jTextFieldNbUnitsPerSymbol;
	}
	
	private JTextField getJTextFieldPointSize() {
		if(jTextFieldPointSize == null) {
			jTextFieldPointSize = new JTextField();
			jTextFieldPointSize.setText( String.valueOf( DEFAULT_POINT_SIZE ) );
			jTextFieldPointSize.addKeyListener(new KeyAdapter() {
				public void keyTyped(KeyEvent e) {
					setDirty( true );
				}
			});
		}
		return jTextFieldPointSize;
	}	
	
	public ThematicMap getThematicMap() {
		return thematicMap;
	}
	
	public void setThematicMap(ThematicMap thematicMap) {
		this.thematicMap = thematicMap;
	}
		
	public void refreshAttrColumnNames() {
		jPanelCDHelper.populateJComboBox( thematicMap, getJComboBoxJoinIdColumn() );
		jPanelCDHelper.populateJComboBox( thematicMap, getJComboBoxDataColumn() );
		
		jPanelCDHelper.selectDefaultJoinIdAndDataColumns( getJComboBoxJoinIdColumn(), getJComboBoxDataColumn() );
	}		
	
	public void refreshData() {
		if ( thematicMap.getCartographicDisplay() instanceof CDDotDensity ) {
			CDDotDensity cd=(CDDotDensity)thematicMap.getCartographicDisplay();
			
			if ( cd != null ) {
				getJComboBoxJoinIdColumn().setSelectedItem( cd.getJoinIdColumnName() );
				getJComboBoxDataColumn().setSelectedItem( cd.getDataColumnName() );
				
				getJComboBoxDistribution().setSelectedItem( cd.getDistribution() );
				
				if ( cd.getNbUnitsPerSymbol() == 0 ) {
					cd.setNbUnitsPerSymbol( DEFAULT_NB_UNITS_PER_SYMBOL );
				}
				
				if ( cd.getPointSize() == 0 ) {
					cd.setPointSize( DEFAULT_POINT_SIZE );
				}
	
				getJTextFieldNbUnitsPerSymbol().setText( Integer.toString( cd.getNbUnitsPerSymbol() ) );
				getJTextFieldPointSize().setText( Integer.toString( cd.getPointSize() ) );
				((JComboBoxColor)getJComboBoxColor()).setSelectedColor( cd.getColor(), false );
				
				setDirty( cd.isDirty() );
			}			
			
		}
	}	
	
	public CartographicDisplay getCartographicDisplay() throws TMapException {
		CDDotDensity cd =(CDDotDensity)CartographicDisplayFactory.getInstance().get( CDTypes.DOT_DENSITY );

		cd.setJoinIdColumnName( (String)getJComboBoxJoinIdColumn().getSelectedItem() );
		cd.setDataColumnName( (String)getJComboBoxDataColumn().getSelectedItem() );
		
		cd.setDistribution( (DotDensityDistributions.Distribs)(getJComboBoxDistribution().getSelectedItem()) );
		
		String nbspu =getJTextFieldNbUnitsPerSymbol().getText();
		try {
			cd.setNbUnitsPerSymbol( Integer.parseInt( nbspu ) );
		} catch( Exception e ) {
			cd.setNbUnitsPerSymbol( DEFAULT_NB_UNITS_PER_SYMBOL );
			getJTextFieldNbUnitsPerSymbol().setText( String.valueOf( cd.getNbUnitsPerSymbol() ) );
		}
		
		String ps =getJTextFieldPointSize().getText();
		try {
			cd.setPointSize( Integer.parseInt( ps ) );
		} catch( Exception e ) {
			cd.setPointSize( DEFAULT_POINT_SIZE );
			getJTextFieldPointSize().setText( String.valueOf( cd.getPointSize() ) );
		}		
		
		cd.setColor( ((JComboBoxColor)getJComboBoxColor()).getSelectedColor() );
		
		cd.setDirty( isDirty() );
		
		return cd;
		
	}
	
	/**
	 * This method initializes jComboBoxJoinIdColumn	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxJoinIdColumn() {
		if (jComboBoxJoinIdColumn == null) {
			jComboBoxJoinIdColumn = new JComboBox();
			jComboBoxJoinIdColumn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setDirty( true );
				}
			});
		}
		return jComboBoxJoinIdColumn;
	}
	
	private JComboBox getJComboBoxDistribution() {
		if (jComboBoxDistribution == null) {
			jComboBoxDistribution = new JComboBox( DotDensityDistributions.Distribs.values() );

			jComboBoxDistribution.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setDirty( true );
				}
			});
		}
		
		return jComboBoxDistribution;
	}	
	
	/**
	 * This method initializes jPanelInfo	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelInfo() {
		if (jPanelInfo == null) {
			
			GridLayout gridLayout = new GridLayout(6, 2, 5, 5);
			jPanelInfo = new JPanel();
			jPanelInfo.setLayout(gridLayout);
			jPanelInfo.add( new JLabel( "Join ID column" ), null);
			jPanelInfo.add(getJComboBoxJoinIdColumn(), null);
			
			jPanelInfo.add( new JLabel( "Data column" ), null);
			jPanelInfo.add(getJComboBoxDataColumn(), null);
			
			jPanelInfo.add( new JLabel( "Distribution" ), null);
			jPanelInfo.add(getJComboBoxDistribution(), null);			
			
			jPanelInfo.add( new JLabel( "# of units per symbol" ), null);
			jPanelInfo.add(getJTextFieldNbUnitsPerSymbol(), null);
			
			jPanelInfo.add( new JLabel( "Point size" ), null);
			jPanelInfo.add(getJTextFieldPointSize(), null);
			
			jPanelInfo.add( new JLabel( "Color") , null);
			jPanelInfo.add(getJComboBoxColor(), null);
		}
		return jPanelInfo;
	}
	
	public boolean isDirty() {
		return dirty;
	}
	
	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}
	
	public String getDataColumnName() {
		return (String)getJComboBoxDataColumn().getSelectedItem();
	}
	
	public String getJoinIdColumnName() {
		return (String)getJComboBoxJoinIdColumn().getSelectedItem();
	}
	
	public void setDataColumnName(String dataColumnName) {
		getJComboBoxDataColumn().setSelectedItem( dataColumnName );
	}

	public void setJoinIdColumnName(String joinIdColumnName) {
		getJComboBoxJoinIdColumn().setSelectedItem( joinIdColumnName );
	}
	
}
